package org.wlw.web.filter;


import org.wlw.web.models.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest hsq = (HttpServletRequest)request;
        HttpServletResponse hsResp = (HttpServletResponse)response;
        User loginUser = (User) hsq.getSession().getAttribute("loginUser");
        if(loginUser==null) {
            hsResp.sendRedirect(hsq.getContextPath()+"/login");
        }
        //过滤器链条往下传递
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
